home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13244 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  52 lines

  1. Path: soap.news.pipex.net!pipex!usenet
  2. From: gs94@dial.pipex.com
  3. Newsgroups: comp.lang.c++
  4. Subject: Which is better????
  5. Date: Sun, 24 Mar 1996 14:01:02 +0000
  6. Organization: UnipalmPIPEX server (post doesn't reflect views of UnipalmPIPEX)
  7. Message-ID: <3155559E.7120@dial.pipex.com>
  8. NNTP-Posting-Host: an164.du.pipex.com
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0GoldB1 (Win95; I)
  13. CC: c.mackay@dcs.napier.ac.uk
  14.  
  15. I have a project where data is passed between functions, as is fairly normal in a 
  16. C++ program. What I would like to know is:
  17.  
  18. Which is better / faster, passing data by pointers or by reference, e.g.
  19.  
  20. void main()
  21. {
  22.     CMyObject theObject;
  23.     
  24.     // Perform function 1 (uses pointer)
  25.     function_1(&theObject);
  26.  
  27.     // Perform function 2 (uses reference)
  28.     function_2(theObject);    
  29. }
  30.  
  31. void function_1( CMyObject* pMyObject )
  32. {
  33.     pMyObject->DoSomethingWithObject();
  34. }
  35.  
  36. void function_2( CMyObject& MyObject )
  37. {
  38.     MyObject.DoSomethingWithObject();
  39. }
  40.  
  41.  
  42. I would prefer to use the faster method as I am iterating over some functions 
  43. several times passing megabytes of data (not all at once, in chunks of a few 
  44. dozen bytes at a time)
  45.  
  46. What is the real difference between the two methods also?
  47.  
  48. Thanks,
  49.  
  50.  
  51. Colin Mackay - gs94@dial.pipex.com
  52.